package com.example.baselib

import android.app.Activity
import android.app.Application
import android.content.Context
import android.os.Bundle
import android.os.CountDownTimer
import android.util.Log
import android.widget.Toast


import com.alibaba.android.arouter.launcher.ARouter
import com.hikvision.open.hikvideoplayer.HikVideoPlayerFactory
import com.tencent.bugly.Bugly
import com.tencent.bugly.beta.Beta


import java.util.ArrayList

import me.jessyan.autosize.AutoSizeConfig


class MyApplication : Application(), Application.ActivityLifecycleCallbacks {
    internal var countActivity = 0
    internal var mCountFlag = false
    private var activities: MutableList<Activity> = ArrayList()

    private val mCountDownTimer: CountDownTimer? = null


    override fun onCreate() {
        super.onCreate()

        // 这两行必须写在init之前，否则这些配置在init过程中将无效
        ARouter.openLog()     // 打印日志
        ARouter.openDebug()   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)

        ARouter.init(this@MyApplication)
        AutoSizeConfig.getInstance().unitsManager
        //全局管理Activity生命周期
        registerActivityLifecycleCallbacks(this)
        Bugly.init(applicationContext, "45dd58b71b", false)//Bugly 集成
//        Beta.autoCheckUpgrade = false
        HikVideoPlayerFactory.initLib("", true)
        //        mCountDownTimer = new CountDownTimer(60 * 1000, 1 * 1000) {
        //            @Override
        //            public void onTick(long millisUntilFinished) {
        //
        //            }
        //
        //            @Override
        //            public void onFinish() {
        //                finishAllActivity();
        //                mCountFlag=false;
        //
        //            }
        //        };
    }

    private fun addActivity(activity: Activity) {
        // 判断当前集合中不存在该Activity
        if (!activities.contains(activity)) {
            activities.add(activity)//把当前Activity添加到集合中
        }
    }

    private fun removeActivity(activity: Activity) {
        //判断当前集合中存在该Activity
        if (activities.contains(activity)) {
            activities.remove(activity)//从集合中移除
            activity.finish()//销毁当前Activity
        }
    }

    private fun finishAllActivity() {
        //判断当前集合中存在该Activity
        for (activity in activities) {
            activity.finish()
        }
    }

    override fun onTerminate() {
        super.onTerminate()
        //ARouter inject 释放
        ARouter.getInstance().destroy()
    }


    override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
        addActivity(activity)
    }

    override fun onActivityStarted(activity: Activity) {
        //        if (mCountFlag){
        //            mCountDownTimer.cancel();
        //            mCountFlag=false;
        //        }
        //        countActivity++;

    }

    override fun onActivityResumed(activity: Activity) {
        Log.i("MyApplication", "onActivityResumed")
    }

    override fun onActivityPaused(activity: Activity) {
        Log.i("MyApplication", "onActivityResumed" + activities.size)
    }

    override fun onActivityStopped(activity: Activity) {

        //        countActivity--;
        //        if (countActivity<=0){
        //            Log.i("MyApplication","退出到后台"+ activities.size());
        //            Toast.makeText(getApplicationContext(),"后台运行，60秒后关闭程序",Toast.LENGTH_LONG).show();
        //            mCountDownTimer.start();
        //            mCountFlag=true;
        //
        //        }
    }

    override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {

    }

    override fun onActivityDestroyed(activity: Activity) {
        removeActivity(activity)
    }
}
